#include "ULUI_LookupSingle.h"
#include "ULUI_PredictByAC.h"
#include "ULGfx_History.h"
#include "ULGfx_CrossHair.h"
#include "ULGfx_Bomb.h"
#include "ULGfx_CrossHair.h"
#include "ULCore_Prediction.h"
#include "ULCore_Algorithm.h"
#include "UL_Global.h"
#include "UL_Tools.h"
#include "VSDL_tools.h"

#define PREDICT_BYAC_START_X	100
#define PREDICT_BYAC_START_Y	100

static ULC_Serial predictedSerial;
static ULC_ACData acData;
static int lastAC;

ULUI_PredictByAC ULUI_PredictByAC::predictByAC;

enum BinLookupIDs{
	EXIT,	
};	

static char *menuTitles[] = {
	"返回"
};

void ULUI_PredictByAC::Init()
{
	int i;
	VSDL_Point currentXY(PREDICT_BYAC_START_X, PREDICT_BYAC_START_Y);
	int w, h;
	ULUI_Menu menu;
	int ret;
	ULG_History *ulgHistory = ULG_History::Instance();

	AttachEventHandler(this);

	menu.SetFont(UL_Global::UnionLottoFont());
	menu.ShowBoarder(true);
	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		menu.SetID(i);
		menu.SetTitle(menuTitles[i]);
		menus.AddMenu(menu);
	}

	menus.SetFirstMenuXY(VSDL_Point(PREDICT_BYAC_START_X, PREDICT_BYAC_START_Y));
	menu.GetTitleSize(w, h);

	UL_Global::UnionLottoPredictByAC(predictedSerial);
	acData.Caculate(&ulgHistory->GetLatestSerial()->sortedBalls);
	lastAC = acData.ac;
}

void ULUI_PredictByAC::CleanUp()
{
	menus.Reset();
}

void ULUI_PredictByAC::Pause()
{
}

void ULUI_PredictByAC::Resume()
{
	selectedMenu = -1;
}

void ULUI_PredictByAC::Update(VSDL_GameEngine *game)
{
	if (selectedMenu == -1){
		return ;
	}

	switch (selectedMenu){
		case EXIT:
			game->PopState();
			break;
	}
}

void ULUI_PredictByAC::Draw(VSDL_GameEngine *game)
{
	VSDL_RGB rgb(100, 100, 100);
	VSDL_Point currentXY(PREDICT_BYAC_START_X, PREDICT_BYAC_START_Y);
	int fontHeight;
	char msgBuffer[256];
	ULG_Serial *ulgSerial = UL_Global::UnionLottoSerial();
	SDL_Surface *destSurf = game->GetSurface();

	UL_Global::UnionLottoShowBg();
	UL_Global::UnionLottoShowTitle();
	menus.Show(destSurf);

	fontHeight = UL_Global::UnionLottoGetFontHeight();
	currentXY.y += fontHeight * (EXIT + 1);
	//Draw result rect
	SPG_RectFilled(destSurf, currentXY.x, currentXY.y, destSurf->w, destSurf->h, rgb.GetRGB32(destSurf));

	UL_Global::UnionLottoShowTip("预测结果(AC预测):", currentXY);
	currentXY.y += fontHeight;
	*ulgSerial = predictedSerial;
	ulgSerial->SetXY(currentXY);
	ulgSerial->Show(destSurf);

	currentXY.x += (destSurf->w >> 1);

	sprintf(msgBuffer, "上期AC: %d", lastAC);
	UL_Global::UnionLottoShowTip(msgBuffer, currentXY);
	currentXY.y += fontHeight;

	acData.Caculate(&predictedSerial.sortedBalls);
	sprintf(msgBuffer, "预测AC: %d", acData.ac);
	UL_Global::UnionLottoShowTip(msgBuffer, currentXY);
	currentXY.y += fontHeight;

	if (retList.size() == 0){
		UL_Global::UnionLottoShowTip("历史数据中没有与该预测重复的结果", currentXY);
	}else{
		ULC_Serial *ulcSerial;

		UL_Global::UnionLottoShowTip("历史数据中与该预测重复的结果有:", currentXY);
		currentXY.y += fontHeight;

		for (iter = retList.begin(); iter != retList.end(); iter++){
			ulcSerial = *iter;

			sprintf(msgBuffer, "期号:% 10d 日期:  %s", ulcSerial->sn, ulcSerial->date);
			UL_Global::UnionLottoShowTip(msgBuffer, currentXY);

			currentXY.y += fontHeight;
		}
	}
}

ULUI_PredictByAC *ULUI_PredictByAC::Instance()
{
	return &predictByAC;
}


void __HANDLE_EVENT ULUI_PredictByAC::OnKeyDown(SDLKey keysym, unsigned char *keyStates, void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	if (keyStates[SDLK_ESCAPE]){
		game->PopState();
	}
}

void __HANDLE_EVENT ULUI_PredictByAC::OnMouseMove(int button, unsigned char buttonState, int absX, int absY, int relX, int relY, void *arg)
{
	int i;

	menus.UpdateMenuList(absX, absY);

}

void __HANDLE_EVENT ULUI_PredictByAC::OnMouseDown(int button, unsigned char buttonState, int absX, int absY, void *arg)
{
	int i;
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	selectedMenu = -1;

	for (i = 0; i <= EXIT; i++){
		ULUI_Menu &menu = menus.GetMenu(i);

		if (menu.IsXYInRect(absX, absY)){
			selectedMenu = i;
			break;
		}
	}

}

void __HANDLE_EVENT ULUI_PredictByAC::OnQuit(void *arg)
{
	VSDL_GameEngine *game = reinterpret_cast<VSDL_GameEngine *>(arg);

	game->Quit();
}
